package system;

import db.DBObject;


public class User implements DBObject {
	long id;
	String account;
	String password;
	
	UserIdentity user_identity;
	
	String name;
	String phone;
	String address;
	
	double credit_points;
	int equal_coupons_count;
	int discount_coupons_count;
	
	String favorite_book_ids;
	
	public User(String account, String password,
			UserIdentity user_identity, String name, String phone,
			String address) {
		this.account = account;
		this.password = password;
		this.user_identity = user_identity;
		this.name = name;
		this.phone = phone;
		this.address = address;
	}

	User() {
	}

	public int getEqual_coupons_count() {
		return equal_coupons_count;
	}

	public int getDiscount_coupons_count() {
		return discount_coupons_count;
	}

	@Override
	public long getId() {
		return id;
	}

	public String getAccount() {
		return account;
	}

	public String getPassword() {
		return password;
	}

	public UserIdentity getUser_identity() {
		return user_identity;
	}

	public String getName() {
		return name;
	}

	public String getPhone() {
		return phone;
	}

	public String getAddress() {
		return address;
	}

	public double getCredit_points() {
		return credit_points;
	}
	
	public void addFavorate(Book pBook) {
		if (this.favorite_book_ids == null) {
			this.favorite_book_ids = "";
		}
		
		long id = pBook.getId();
		this.favorite_book_ids = this.favorite_book_ids + " " + id;
	}
	
	public void removeFavorate(Book pBook) {
		if (this.favorite_book_ids == null) return;
		
		String[] ids = this.favorite_book_ids.split(" ");
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < ids.length; i++) {
			if (Long.parseLong(ids[i]) != pBook.getId()) {
				buffer.append(ids[i] + " ");
			}
		}
		
		if (buffer.length() > 0) buffer.delete(buffer.length() - 1, buffer.length());
		
		this.favorite_book_ids = buffer.toString();
	}
	
	@Override
	public boolean equals(Object pObject) {
		if (pObject instanceof User) {
			return ((User) pObject).id == this.id;
		}
		
		return false;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setUser_identity(UserIdentity user_identity) {
		this.user_identity = user_identity;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public void setCredit_points(double credit_points) {
		this.credit_points = credit_points;
	}

	public void setEqual_coupons_count(int equal_coupons_count) {
		this.equal_coupons_count = equal_coupons_count;
	}

	public void setDiscount_coupons_count(int discount_coupons_count) {
		this.discount_coupons_count = discount_coupons_count;
	}

	@Override
	public int hashCode() {
		return (int) id;
	}
	
}
